﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace 抽奖小程序
{
    public struct student
    {
       public  string name;
        public double money;
    };
    
    
    public partial class form1 : Form
    {
        public static student[] nowstudent=new student[105];
        System.DateTime TimeNow = new DateTime();
        System.DateTime playTimeNow = new DateTime();
        TimeSpan TimeCount = new TimeSpan();
        TimeSpan playTimeCount = new TimeSpan();
        int moniterflag = 0;
        double minmoney;
        double maxmoney;
        double resultmoney;
        public form1()
        {
           
            InitializeComponent();
            label6.Visible = false;
            textBox2.Visible = false;
            button7.Visible = false;
            setstu();
            label5.Text = "让我们快乐的开始抽红包吧";
            int x = (int)(0.5 * (this.Width - label5.Width));
            label5.Location = new Point(x, label5.Location.Y);
            x = (int)(0.5 * (this.Width - trsut.Width));
            trsut.Location = new Point(x, trsut.Location.Y);
            x= (int)(0.5 * (this.Width - pictureBox1.Width));
            pictureBox1.Location =  new Point(x, pictureBox1.Location.Y);
            
            trsut.Visible = false;//设置控件隐藏；
            Bitmap b = new Bitmap(Application.StartupPath + @"\log\" + 0 + ".png");
            pictureBox1.Image = b;
           
            //this.BackgroundImage.
        }

        private void button1_Click(object sender, EventArgs e)//开始计时
        {
            //DateTime dt = System.DateTime.Now;
            //textBox3.Text = dt.ToString();
            label6.Visible = false;
            textBox2.Visible = false;
            button7.Visible = false;
            label5.ForeColor = Color.Black;
            textBox1.Text = "";
            int originalMinMoney = 5;
            int originalMaxMoney = 10;
            timer1.Start();
            TimeNow = DateTime.Now;
            minMoney.Text = originalMinMoney.ToString();
            maxMoney.Text = originalMaxMoney.ToString();
            label5.Text = "下一个幸运儿是谁呢？？？";
            int x = (int)(0.5 * (this.Width - label5.Width));
            label5.Location = new Point(x, label5.Location.Y);
            trsut.Visible = false;//设置控件隐藏；
        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Stop();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            TimeCount = DateTime.Now - TimeNow;
            textBox3.Text = string.Format("{0:00}:{1:00}:{2:00}", TimeCount.Hours, TimeCount.Minutes, TimeCount.Seconds);
           // textBox3.Text = TimeCount.Seconds.ToString();
            int ss = (int)TimeCount.TotalSeconds;
            //ss = DateTime.Now.Second-TimeNow.Second;
            //textBox3.Text = ss.ToString();
            if (ss < 300)
            {
                minmoney = (ss / 5) * 0.05 + 10;
                maxmoney = (ss / 5) * 0.08 + 20;
            }
            else if (ss >= 300&&ss<=600)
            {
                minmoney = (ss / 5) * 0.10 + 10;
                maxmoney = (ss / 5) * 0.15 + 20;
               
                
            }
            else
            {
                minmoney = (ss / 5) * 0.13 + 10;
                maxmoney = (ss / 5) * 0.30 + 20;
                
            }
            //设置显示器内容
            if (minmoney < 12)
            {
                minMoney.ForeColor = Color.Black;
                maxMoney.ForeColor = Color.Black;
            }
            else if (minmoney >= 12 && minmoney < 20)
            {//状态为1；
                minMoney.ForeColor = Color.Gold;
                maxMoney.ForeColor = Color.Gold;
                int x = (int)(0.5 * (this.Width - label5.Width));
                label5.Location = new Point(x, label5.Location.Y);
                label5.ForeColor = Color.Gold;
                label5.Text = "已经"+maxmoney.ToString("0.00")+"元，申老师的钱包跃跃欲动";
            }
            else if(minmoney>=20&&minmoney<30){
                //状态2
                minMoney.ForeColor = Color.Coral;
                maxMoney.ForeColor = Color.Coral;
                int x = (int)(0.5 * (this.Width - label5.Width));
                label5.Location = new Point(x, label5.Location.Y);
                label5.ForeColor = Color.Coral;
                label5.Text = "已经" + maxmoney.ToString("0.00") + "元，申老师压不住自己的口袋了";
            }
            else if (minmoney >= 15 && minmoney < 20)
            {
                //状态3
                minMoney.ForeColor = Color.OrangeRed;
                maxMoney.ForeColor = Color.OrangeRed;
                int x = (int)(0.5 * (this.Width - label5.Width));
                label5.Location = new Point(x, label5.Location.Y);
                label5.ForeColor = Color.OrangeRed;
                label5.Text = "已经" + maxmoney.ToString("0.00") + "元，我已经感觉到了周围饥渴的眼神";
            }
            else
            {
                //状态4
                minMoney.ForeColor = Color.Red;
                maxMoney.ForeColor = Color.Red;
                int x = (int)(0.5 * (this.Width - label5.Width));
                label5.Location = new Point(x, label5.Location.Y);
                label5.ForeColor = Color.DarkRed;
                label5.Text = "已经" + maxmoney.ToString("0.00") + "元，再不拿主持人就要私吞红包啦!!!!";
            }
            minMoney.Text = minmoney.ToString();
            maxMoney.Text = maxmoney.ToString();
        }

        private void form1_Load(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            int x = (int)(0.5 * (this.Width - label5.Width));
            label5.Location = new Point(x, label5.Location.Y);
            label5.Text = ("红包已经打包成功");
            trsut.Visible = true;
            int playtimess = (int)playTimeCount.TotalSeconds;
                Random ra = new Random();
                Random rb = new Random();
                int i = ra.Next(1, 100);
                switch (playtimess / 60)
                {
                    case 0:
                    case 1:
                        {
                            if (i < 25)
                            {
                                resultmoney = minmoney;
                            }
                            else if (i>=25&&i<80)
                            {
                                resultmoney = minmoney + (maxmoney - minmoney) * 0.25;
                            }
                            else if (i>=80&&i<90)
                            {
                                resultmoney = minmoney + (maxmoney - minmoney) * 0.5;

                            }
                            else if (i >=90&& i < 97)
                            {
                                resultmoney = minmoney + (maxmoney - minmoney) * 0.75;
                            }
                            else
                            {
                                resultmoney = maxmoney;
                            }
                            break;
                        }
                    case 2:
                    case 3:
                    case 4:
                        {
                            if (i < 10)
                            {
                                resultmoney = minmoney;
                            }
                            else if (i>=10&&i<25)
                            {
                                resultmoney = minmoney + (maxmoney - minmoney) * 0.25;
                            }
                            else if (i>=25&&i<65)
                            {
                                resultmoney = minmoney + (maxmoney - minmoney) * 0.5;

                            }
                            else if (i >=65&& i < 85)
                            {
                                resultmoney = minmoney + (maxmoney - minmoney) * 0.75;
                            }
                            else
                            {
                                resultmoney = maxmoney;
                            }
                            break;
                        }
                    case 5:
                    case 6:
                    case 7:
                    case 8:
                    case 9:
                        {
                            if (i < 3)
                            {
                                resultmoney = minmoney;
                            }
                            else if (i>=3&&i<15)
                            {
                                resultmoney = minmoney + (maxmoney - minmoney) * 0.25;
                            }
                            else if (i>=15&&i<30)
                            {
                                resultmoney = minmoney + (maxmoney - minmoney) * 0.5;

                            }
                            else if (i >=30&& i < 55)
                            {
                                resultmoney = minmoney + (maxmoney - minmoney) * 0.75;
                            }
                            else
                            {
                                resultmoney = maxmoney;
                            }
                            break;
                        }
                    default:
                        {
                            double temp=rb.NextDouble();
                            while (temp > 0.5)
                            {
                                temp = rb.NextDouble();
                            }

                            resultmoney = maxmoney + maxmoney*temp;
                            break;
                        }
                }
               
            
        }
        private void label5_Click(object sender, EventArgs e)
        {

        }
        private void trsut_Click(object sender, EventArgs e)
        {
            label6.Visible = true;
            textBox2.Visible = true;
            button7.Visible = true;
            trsut.Visible = false;
            int x = (int)(0.5 * (this.Width - label5.Width));
            label5.Location = new Point(x, label5.Location.Y);
            label5.Text = ("恭喜你获得了"+resultmoney.ToString("0.00")+"元");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            timer2.Start();
            playTimeNow = DateTime.Now;
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            playTimeCount = DateTime.Now - playTimeNow;
            textBox1.Text = string.Format("{0:00}:{1:00}:{2:00}", playTimeCount.Hours, playTimeCount.Minutes, playTimeCount.Seconds);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            timer2.Stop();
        }
        private void setmoniterstatus(int n)
        {
            if (n == moniterflag) return;
            else
            {
                moniterflag = n;
               /* switch (n)
                {
                    case 0:
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                }*/
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            form2.Show();
        }
        private void setstu()
        {
            string[] name = new string[41]{"xx","xxx","xx1","xx2","xx3","xx4","xx5","xx6","xx7","xx8",
                "xx9","xx11","xx12","xx13","xx14","xx15","xx15","xx15","xx15","xx15","xx15","xx15","xx15","xx15",
"xx15","xx15","xx15","xx15","xx15","xx15","xx15","xx15","xx15","xx15","xx15","xx15","xx15","xx15","xx15","xx15","xx15"};
           for(int j = 0; j < 41; j++)
            {
                nowstudent[j] = new student();
                nowstudent[j].name = name[j];
                nowstudent[j].money = 0;
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            string temp = textBox2.Text;
            string[] rewordpeople = temp.Split(',');
            for(int j = 0; j < rewordpeople.Length; j++)
            {
                for(int k = 0; k < 41; k++)
                {
                    if (rewordpeople[j] == nowstudent[k].name)
                    {
                        nowstudent[k].money += resultmoney/ rewordpeople.Length;
                    }
                }
            }
            label6.Visible = false;
            textBox2.Visible = false;
            button7.Visible = false;
            MessageBox.Show("添加成功");
        }

        private void button8_Click(object sender, EventArgs e)
        {
            int tempk = 0;
            double min = 10000;
            string rewordname="";
            string[] nomoney = new string[50];
            for (int j = 0; j < 41; j++)
            {
                if (nowstudent[j].money == 0)
                {
                    nomoney[tempk] = nowstudent[j].name;
                    tempk++;
                }
                else
                {
                    if (min < nowstudent[j].money) rewordname = nowstudent[j].name;
                }
            }
            if (tempk!=0)
            {
                Random ra = new Random();
                int i = ra.Next(1, tempk - 1);
                MessageBox.Show("恭喜" + nomoney[i] + "获得神秘大奖");
            }
            else
            {
                MessageBox.Show("恭喜" + rewordname + "获得神秘大奖");
            }
            
        }

        private void button9_Click(object sender, EventArgs e)
        {

            FileStream fs = new FileStream("test.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
            StreamWriter sw = new StreamWriter(fs); // 创建写入流
            sw.WriteLine("姓名                红包金额");
            for (int j = 0; j < 41; j++)
            {
                string temp = nowstudent[j].name +"                     "+ nowstudent[j].money.ToString();
                sw.WriteLine(temp); // 写入Hello World
            }
            sw.Close();
            MessageBox.Show("存档成功");
        }
    }
}
